package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 勾新杰
 * @version 1.0
 * @date 2024/10/14 21:39
 */
@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue"), // 支付成功不一定只给trade微服务发送消息
            exchange = @Exchange(name = "pay.direct"), // 支付服务对应一个交换机负责转发
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        // 查询订单状态
        Order order = orderService.getById(orderId);
        // 判断状态是否不是未支付
        if (order == null || order.getStatus() != 1){
            return;
        }
        // 如果是未支付修改订单状态
        orderService.markOrderPaySuccess(orderId);
    }
}
